<template>
  <el-dialog title="添加经纪" :visible="isShowDialog" :destroy-on-close="true" @close="btnCancel">
    <el-form label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="formData.address" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" />
      </el-form-item>
      <el-form-item label="封面" prop="image">
        <ImageUpLoad ref="upload" />
        <el-input placeholder="强制设置图片跳过上传" />
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button size="small" type="primary" @click="btnOK">确定</el-button>
      <el-button size="small" @click="btnCancel">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { addAgents, getAgents, editAgents } from '@/api/agents'
import ImageUpLoad from '@/components/ImageUpload/index.vue'

export default {
  components: {
    ImageUpLoad
  },
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
    }
  },
  methods: {
    // 新增 确认
    async btnOK() {
      //
      if (!this.$refs.upload.fileList[0]) return this.$message.error('请先上传图片')
      if (this.$refs.upload.fileList[0].status !== 'success') return this.$message.error('请等待上传完毕')
      if (this.formData.id) {
        // 编辑
        await editAgents({
          ...this.formData,
          image: this.$refs.upload.fileList[0].url
        })
      } else {
        // 新增
        await addAgents({
          ...this.formData,
          image: this.$refs.upload.fileList[0].url
        })
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.$emit('update:isShowDialog', false)
      // 更新页面
      this.$emit('reloadPage')
    },
    btnCancel() {
      // 清空数据
      this.formData = {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
      this.$refs.upload.fileList = []
      // 关闭弹窗
      this.$emit('update:isShowDialog', false)
    },
    async getAgents(id) {
      this.formData = await getAgents(id)
      this.$refs.upload.fileList = [{ url: this.formData.image }]
    }
  }
}
</script>

<style lang=scss scoped>

</style>
